package com.xueyi.aixihu.service.impl;

import com.alibaba.fastjson.JSON;
import com.xueyi.aixihu.config.SpringContextConfig;
import com.xueyi.aixihu.constant.*;
import com.xueyi.aixihu.entity.dto.AuditNewsDto;
import com.xueyi.aixihu.entity.dto.NewsDto;
import com.xueyi.aixihu.entity.generatedMapper.CdzAuditRecordDao;
import com.xueyi.aixihu.entity.generatedMapper.DtNewsMapper;
import com.xueyi.aixihu.entity.generatedMapper.DtUsersMapper;
import com.xueyi.aixihu.entity.po.*;
import com.xueyi.aixihu.entity.vo.*;
import com.xueyi.aixihu.exception.SzException;
import com.xueyi.aixihu.mapper.LocalDtNewsMapper;
import com.xueyi.aixihu.mapper.MenuMapper;
import com.xueyi.aixihu.pattern.CheckOperationArticleRoleStrategy;
import com.xueyi.aixihu.pattern.CheckOperationArticleRoleStrategyDirector;
import com.xueyi.aixihu.pattern.CheckOperationArticleRoleStrategy;
import com.xueyi.aixihu.reqeust.*;
import com.xueyi.aixihu.response.ArticleListResponse;
import com.xueyi.aixihu.response.FindPartyPolicyNewsResponse;
import com.xueyi.aixihu.response.RemoveArticleAreaReq;
import com.xueyi.aixihu.service.DtNewsService;
import com.xueyi.aixihu.status.IsStatus;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.Base64DecodedMultipartFile;
import com.xueyi.aixihu.utils.LogUtil;
import com.xueyi.aixihu.utils.MD5Utils;
import com.xueyi.aixihu.utils.WeChatDevUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @authoer:hxs
 * @createDate:2022/8/16
 * @description: 文章操作业务
 */
@Service
@Slf4j
public class DtNewsServiceImpl implements DtNewsService {
    @Autowired
    private LocalDtNewsMapper dtNewsMapper;

    @Autowired
    private DtUsersMapper dtUsersMapper;

    @Autowired
    private CdzAuditRecordDao cdzAuditRecordDao;

//    /**
//     * 条件查询文章列表
//     *
//     * @param changeClassifyReq
//     * @return
//     */
//    @Override
//    public ArticleListResponse findArticleList(ChangeClassifyReq changeClassifyReq) throws SzException {
//        changeClassifyReq.format();
//        changeClassifyReq.countStartIndex();
//
//        //查询当前账户管理当区域
//        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
//        String userManageAreaId = null;
//        if (currentUser != null) {
//            userManageAreaId = currentUser.findUserManageAreaId();
//        }
////        String userManageAreaId = DtUsersVo.findCurrentUser().findUserManageAreaId();
//
//
//        List<DtNewsVo> dtNewsVoList = new ArrayList<>();
//        Integer total = 0;
//
//
//        //查询所有的文章
//        if (null == changeClassifyReq.getOneClassifyId() && null == changeClassifyReq.getTwoClassifyId() && null == changeClassifyReq.getThreeClassifyId() && null == changeClassifyReq.getFourClassifyId()) {
//            total = dtNewsMapper.findAllArticleListTotal(changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
//            if (total > 0) {
//                dtNewsVoList = dtNewsMapper.findAllArticleList(userManageAreaId, changeClassifyReq.getStartIndex(), changeClassifyReq.getLimit(), changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
//            }
//        } else {
//
//            String ids = "";
//
//            //通过四级分类查询文章列表
//            if (null != changeClassifyReq.getFourClassifyId()) {
//                ids += changeClassifyReq.getFourClassifyId();
////                total = dtNewsMapper.findArticleListTotal(ids, changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
////                if (total > 0) {
////                    dtNewsVoList = dtNewsMapper.findArticleList(ids, userManageAreaId, changeClassifyReq.getStartIndex(),
////                            changeClassifyReq.getLimit(), changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
////                }
//            }
//            //通过三级分类查询文章列表
//            else if (null != changeClassifyReq.getThreeClassifyId()) {
//                ids = dtNewsMapper.findThreeIds(changeClassifyReq.getThreeClassifyId());
//                if (StringUtils.isBlank(ids)) {
//                    ids = changeClassifyReq.getThreeClassifyId() + "";
//                } else {
//                    ids += "," + changeClassifyReq.getThreeClassifyId();
//                }
//
////                total = dtNewsMapper.findTwoArticleListTotal(ids, changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
////                if (total > 0) {
////                    dtNewsVoList = dtNewsMapper.findTwoArticleList(ids, userManageAreaId, changeClassifyReq.getStartIndex(),
////                            changeClassifyReq.getLimit(), changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
////                }
//
//            }
//            //通过二级分类查询文章列表
//            else if (null != changeClassifyReq.getTwoClassifyId()) {
//
//                String threeIds = dtNewsMapper.findThreeIds(changeClassifyReq.getTwoClassifyId());
//                if (!StringUtils.isBlank(threeIds)) {
//                    ids += threeIds;
//                    String fourIds = dtNewsMapper.findThreeIdsFun(threeIds);
//                    if (!StringUtils.isBlank(fourIds)) {
//                        ids += "," + fourIds;
//                    }
//
//                }
//
//                if (StringUtils.isBlank(ids)) {
//                    ids = changeClassifyReq.getTwoClassifyId() + "";
//                } else {
//                    ids += "," + changeClassifyReq.getTwoClassifyId();
//                }
////                total = dtNewsMapper.findOneArticleListTotal(ids, changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
////                if (total > 0) {
////                    dtNewsVoList = dtNewsMapper.findOneArticleList(ids, userManageAreaId,
////                            changeClassifyReq.getStartIndex(), changeClassifyReq.getLimit(), changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
////                }
//            }
//            //通过以及分类查询文章列表
//            else if (null != changeClassifyReq.getOneClassifyId()) {
//                String twoIds = dtNewsMapper.findThreeIds(changeClassifyReq.getOneClassifyId());
//                if (!StringUtils.isBlank(twoIds)) {
//                    ids += twoIds;
//                    String threeIds = dtNewsMapper.findThreeIdsFun(twoIds);
//                    if (!StringUtils.isBlank(threeIds)) {
//                        ids += "," + threeIds;
//                        String fourIds = dtNewsMapper.findThreeIdsFun(threeIds);
//                        if (!StringUtils.isBlank(fourIds)) {
//                            ids += "," + fourIds;
//                        }
//                    }
//                }
//
//
//                if (StringUtils.isBlank(ids)) {
//                    ids = changeClassifyReq.getOneClassifyId() + "";
//                } else {
//                    ids += "," + changeClassifyReq.getOneClassifyId();
//                }
//
//
//            }
//
//            total = dtNewsMapper.findOneArticleListTotal(ids, changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
//            if (total > 0) {
//                dtNewsVoList = dtNewsMapper.findOneArticleList(ids, userManageAreaId,
//                        changeClassifyReq.getStartIndex(), changeClassifyReq.getLimit(), changeClassifyReq.getTitle(), changeClassifyReq.getStatus());
//            }
//
//        }
//
//        ArticleListResponse response = new ArticleListResponse().dtNewsVoList(dtNewsVoList).total(total);
//
//        return response;
//    }

    /**
     * 条件查询文章列表
     *
     * @param changeClassifyReq
     * @return
     */
    @Override
    public ArticleListResponse findArticleList(ChangeClassifyReq changeClassifyReq) throws SzException {


        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        //获取用户的角色
        Integer userMaxRole = dtUsersVo.findUserMaxRole();

        //获取二务（镇党务、镇政务、村党务、村村务）角色策略，判断当前角色是否之处操作
        CheckOperationArticleRoleStrategy roleAdapter = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(changeClassifyReq.getContentType());
        if (roleAdapter == null || !roleAdapter.commonSelect(userMaxRole)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有权限");
        }

        if (changeClassifyReq.getSystemTypeId().isEmpty())
            changeClassifyReq.setSystemTypeId(SystemRoleAuditAreaEnum.getInstance(userMaxRole).getMANAGE_AUDIT_SYSTEM_TYPE_ID());

        //1.初始化参数
        changeClassifyReq.format();
        changeClassifyReq.countStartIndex();

        //获取管辖区域ID
        String id = CheckOperationArticleRoleStrategyDirector.getAreaIdByRoleId(dtUsersVo);
        changeClassifyReq.setUserId(dtUsersVo.getId());
        changeClassifyReq.setRoleId(dtUsersVo.getUserMaxRole());

        //获取文章状态总数目信息
        ArticleListResponse articleListResponse = dtNewsMapper.findContentListCount(changeClassifyReq, id);

        //获取文章列表信息
        List<DtNewsVo> dtNewsVoList = dtNewsMapper.findContentList(changeClassifyReq, id);

        //封装数据
        articleListResponse.setDtNewsVoList(dtNewsVoList);
        articleListResponse.total();
        return articleListResponse;

    }

    public Map<String, Object> findNewsByOne(Integer contentId, Integer showType) {
        DtNewsVo dtNews = dtNewsMapper.findDtNewsById(contentId);
        //获取文章创建者
        DtUsers dtUsers = dtNewsMapper.selectUserById(dtNews.getCreateUserId());

        //获取文章审核日志信息列表
        List<CdzAuditRecord> cdzAuditRecords = dtNewsMapper.selectNewsAuditRecord(contentId);

        //封装审核进度信息列表
        List<AxhAuditFinanceLogVo> auditProcessList = new ArrayList<>();

        //创建3个阶段对象信息
        AxhAuditFinanceLogVo oneProcess = new AxhAuditFinanceLogVo();
        oneProcess.setUsername(dtUsers.getUsername());

        AxhAuditFinanceLogVo twoProcess = new AxhAuditFinanceLogVo();

        CheckOperationArticleRoleStrategy roleStrategy = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(dtNews.getContentType());
        String twoAuditUserName = roleStrategy.oneLevelAuditMemberInfo(dtNews.getSystemTypeId(), StringUtils.isEmpty(dtNews.getMediaId()) ? null : Arrays.asList(dtNews.getMediaId().toString().split(",")).stream().map(e -> Integer.parseInt(e.toString())).collect(Collectors.toList()));
        twoProcess.setUsername(StringUtils.isEmpty(twoAuditUserName) ? dtUsers.getUsername() : twoAuditUserName);

        AxhAuditFinanceLogVo threeProcess = new AxhAuditFinanceLogVo();
        threeProcess.setUsername("发布");
        if (cdzAuditRecords.isEmpty()) {

            oneProcess.setWait(0);
            oneProcess.setWaitStr("待提交");

            twoProcess.setWait(1);
            twoProcess.setWaitStr("");

            threeProcess.setWait(1);
            threeProcess.setWaitStr("");

        } else {

            //判断最后一次操作记录
            CdzAuditRecord lastOperationRecord = cdzAuditRecords.get(cdzAuditRecords.size() - 1);

            if (lastOperationRecord.getCurrentStatus().equals(OneLevelAuditConstant.PUBLIC)) {

                //获取已提交的记录列表
                List<CdzAuditRecord> submitList = cdzAuditRecords.stream().filter(e -> e.getCurrentStatus().equals(OneLevelAuditConstant.SUBMIT_AUDIT)).collect(Collectors.toList());
                oneProcess.setWait(0);
                oneProcess.setWaitStr("已提交");
                oneProcess.setCreateTime(submitList.get(submitList.size() - 1).getLastCommandTime());

                //获取已通过的记录列表
                List<CdzAuditRecord> auditPassedList = cdzAuditRecords.stream().filter(e -> e.getCurrentStatus().equals(OneLevelAuditConstant.CONSENT)).collect(Collectors.toList());
                twoProcess.setWait(0);
                twoProcess.setWaitStr("已通过");
                twoProcess.setCreateTime(auditPassedList.get(auditPassedList.size() - 1).getLastCommandTime());

                threeProcess.setWait(lastOperationRecord.getIsPublic() == 1 ? 0 : 1);
                threeProcess.setWaitStr(lastOperationRecord.getIsPublic() == 1 ? "已发布" : "待发布");
                threeProcess.setCreateTime(lastOperationRecord.getIsPublic() == 1 ? lastOperationRecord.getLastCommandTime() : null);

            } else if (lastOperationRecord.getCurrentStatus() > OneLevelAuditConstant.SUBMIT_AUDIT) {

                //获取已提交的记录列表
                List<CdzAuditRecord> submitList = cdzAuditRecords.stream().filter(e -> e.getCurrentStatus().equals(OneLevelAuditConstant.SUBMIT_AUDIT)).collect(Collectors.toList());
                oneProcess.setWait(0);
                oneProcess.setWaitStr("已提交");
                oneProcess.setCreateTime(submitList.get(submitList.size() - 1).getLastCommandTime());

                twoProcess.setWait(lastOperationRecord.getCurrentStatus().equals(OneLevelAuditConstant.FAIL) ? 3 : 0);
                twoProcess.setWaitStr(lastOperationRecord.getCurrentStatus().equals(OneLevelAuditConstant.FAIL) ? "已驳回" : "已通过");
                twoProcess.setCreateTime(lastOperationRecord.getLastCommandTime());
                twoProcess.setRemark(lastOperationRecord.getFailMsg());

                threeProcess.setWait(lastOperationRecord.getCurrentStatus().equals(OneLevelAuditConstant.FAIL) ? 1 : 0);
                threeProcess.setWaitStr(lastOperationRecord.getCurrentStatus().equals(OneLevelAuditConstant.FAIL) ? "" : "待发布");

            } else if (lastOperationRecord.getCurrentStatus() == OneLevelAuditConstant.SUBMIT_AUDIT) {

                //获取已提交的记录列表
                oneProcess.setWait(0);
                oneProcess.setWaitStr("已提交");
                oneProcess.setCreateTime(lastOperationRecord.getLastCommandTime());

                twoProcess.setWait(0);
                twoProcess.setWaitStr("待审核");

                threeProcess.setWait(1);
                threeProcess.setWaitStr("");

            } else {

                oneProcess.setWait(0);
                oneProcess.setWaitStr("待提交");

                twoProcess.setWait(1);
                twoProcess.setWaitStr("");

                threeProcess.setWait(1);
                threeProcess.setWaitStr("");
            }
        }
        auditProcessList.add(oneProcess);
        auditProcessList.add(twoProcess);
        auditProcessList.add(threeProcess);

        //封装文章信息、审核进度信息
        Map<String, Integer> map1 = roleStrategy.detailBtn(dtNews);
        if (showType.equals(2) && map1.get("isSuccess").equals(1)) {
            map1.put("isSuccess", 0);
            map1.put("isTurn", 0);
        }
        Map<String, Object> newMap = new HashMap<>();
        newMap.put("news", dtNews);
        newMap.put("process", auditProcessList);
        newMap.put("failMsg", twoProcess.getRemark());
        newMap.put("btn", map1);
        return newMap;
    }

    /**
     * 删除文章
     *
     * @param contentId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateArticleStatus(Integer contentId) throws SzException {

        //通过文章id查询文章详情
        DtNewsVo dtNews = dtNewsMapper.findDtNewsById(contentId);
        if (null == dtNews) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有找到该文章");
        }
        //修改文章展示状态
        dtNewsMapper.updateArticleStatus(contentId);
    }

    /**
     * 新增文章
     *
     * @param dtNewsReq
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addArticle(DtNewsReq dtNewsReq) throws SzException, ParseException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        //获取用户的角色
        Integer userMaxRole = dtUsersVo.findUserMaxRole();

        //角色校验
        CheckOperationArticleRoleStrategy roleAdapter = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(dtNewsReq.getContentType());
        if (roleAdapter == null || !roleAdapter.edit(userMaxRole)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有权限");
        }
        if (dtNewsReq.getContentType().equals(ContentConstant.ZJ_ZW)) {
            dtNewsReq.setRoleIds(SystemRoleAuditAreaEnum.getInstance(userMaxRole).getNEXT_AUDIT_ROLE_ID().toString());
        }

        String id = CheckOperationArticleRoleStrategyDirector.getAreaIdByRoleId(dtUsersVo);
        dtNewsReq.setContent(Base64DecodedMultipartFile.operationBase64ImageInfo(dtNewsReq.getContent()));
        WxArticleVo wxArticleVo = new WxArticleVo();
        BeanUtils.copyProperties(dtNewsReq, wxArticleVo);
        wxArticleVo.setNewType(1);
        wxArticleVo.setMediaId(StringUtils.isEmpty(dtNewsReq.getRoleIds()) ? null : dtNewsReq.getRoleIds());
        wxArticleVo.setCreateUserId(dtUsersVo.getId());
        wxArticleVo.setCreateTimeNew(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dtNewsReq.getCreateTime()));
        wxArticleVo.setFromToAreaId(Integer.parseInt(id));

        //插入到数据库
        dtNewsMapper.addArticle(wxArticleVo);
    }

    /**
     * 编辑文章
     *
     * @param dtNewsReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateArticle(DtNewsReq dtNewsReq) throws SzException, ParseException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        //获取用户的角色
        Integer userMaxRole = dtUsersVo.findUserMaxRole();

        //通过文章id查询文章详情
        DtNewsVo dtNews = dtNewsMapper.findDtNewsById(dtNewsReq.getId());
        if (null == dtNews) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有找到该文章");
        }

        //角色校验
        CheckOperationArticleRoleStrategy roleAdapter = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(dtNewsReq.getContentType());
        if (roleAdapter == null || !roleAdapter.edit(userMaxRole)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有权限");
        }

        //封装参数
        dtNewsReq.setContent(Base64DecodedMultipartFile.operationBase64ImageInfo(dtNewsReq.getContent()));
        dtNews.setCreateTimeNew(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dtNewsReq.getCreateTime()));
        dtNews.setThumb(dtNewsReq.getImg());
        dtNews.setImg(dtNewsReq.getImg());
        dtNews.setTitle(dtNewsReq.getTitle());
        dtNews.setContent(dtNewsReq.getContent());
        dtNews.setSystemTypeId(dtNewsReq.getSystemTypeId());
        dtNews.setMediaId(StringUtils.isEmpty(dtNewsReq.getRoleIds()) ? null : dtNewsReq.getRoleIds());
        dtNewsMapper.updateArticle(dtNews);
    }

    /**
     * 回显文章数据
     *
     * @param findArticleReq
     * @return
     */
    @Override
    public ArticleVo findArticleById(FindArticleReq findArticleReq) throws SzException {
        findArticleReq.format();
        return dtNewsMapper.findArticleById(findArticleReq);
    }


    /**
     * 微信公众号的openId
     */
    private static final String WX_GZH_APP_ID = "wx58e6bbc5cda8c37d";
    /**
     * 微信公众号的密钥
     */
    private static final String WX_GZH_SECRET = "d72ae959dfcf284c747b0d9b5859b276";

    /**
     * 获取微信文章并同步到数据库
     *
     * @param wxArticleReq
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<WxArticleVo> findWxArticleFromWx(WxArticleReq wxArticleReq) throws SzException {
        AccessTokenClass assessToken = WeChatDevUtil.getAssessToken(WX_GZH_APP_ID, WX_GZH_SECRET);

        //查看数据库中有没有
        AxhWxNewsIndex axhWxNewsIndex = dtNewsMapper.findWxNewsIndexByType(wxArticleReq.getType());
        if (null == axhWxNewsIndex) {
            //新增该类型索引
            axhWxNewsIndex = new AxhWxNewsIndex();
            axhWxNewsIndex.setNewIndex(0);
            axhWxNewsIndex.setNewType(wxArticleReq.getType());
            dtNewsMapper.addWxNewsIndex(axhWxNewsIndex);
        }

        //获取微信文章
        WxNewsListVo wxArticleList = WeChatDevUtil.getWxArticleList(assessToken.getAccess_token(), wxArticleReq.getType(),
                axhWxNewsIndex.getNewIndex(), wxArticleReq.getCount());
        List<WxItemVo> item = wxArticleList.getItem();
        List<WxArticleVo> articleVos = new ArrayList<>();
        Integer size = dtNewsMapper.findCount();
        for (WxItemVo wxItemVo : item) {
            //封装数据
            size++;
            WxArticleVo wxArticleVo = new WxArticleVo();
            wxArticleVo.setTitle(wxItemVo.getContent().getNews_item().get(0).getTitle());
            wxArticleVo.setThumb(wxItemVo.getContent().getNews_item().get(0).getThumb_url());
            wxArticleVo.setImg(wxItemVo.getContent().getNews_item().get(0).getThumb_url());
            wxArticleVo.setContent(wxItemVo.getContent().getNews_item().get(0).getContent());
            wxArticleVo.setCreateTimeNew(new Date(Long.valueOf(wxItemVo.getContent().getCreate_time()) * 1000));
            wxArticleVo.setCreateTime(String.valueOf((wxItemVo.getContent().getCreate_time())));
            wxArticleVo.setStatus(0);
            wxArticleVo.setPaixu(size + "");
            wxArticleVo.setNewType(2);
            wxArticleVo.setIsVisible(0);
            wxArticleVo.setMediaId(wxItemVo.getMedia_id());
            wxArticleVo.setOriginalUrl(wxItemVo.getContent().getNews_item().get(0).getUrl());
            wxArticleVo.setWxNewJson(JSON.toJSONString(wxItemVo));
            articleVos.add(wxArticleVo);
        }

        //批量插入到数据库
        dtNewsMapper.batchAddWxArticle(articleVos);

        //修改数据库到索引
        axhWxNewsIndex.setNewIndex(axhWxNewsIndex.getNewIndex() + articleVos.size());
        dtNewsMapper.updateWxNewsIndexByType(axhWxNewsIndex);


        StringBuilder ids = new StringBuilder();
        for (WxArticleVo articleVo : articleVos) {
            ids.append(articleVo.getId()).append(",");
        }
        String otherId = ids.substring(0, ids.length() - 1);

        //获取当前登入用户
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        //添加日志
        LogUtil.buildUserLog(otherId,
                "【文章管理】 用户 {" + currentUser.getUsername() + "} 从微信获取了微信文章内容"
                , currentUser, null);

        return articleVos;
    }

    /**
     * 获取微信文章列表
     *
     * @param findWxArticlesReq
     * @return
     */
    @Override
    public List<WxArticleVo> findWxArticles(FindWxArticlesReq findWxArticlesReq) throws SzException {
        findWxArticlesReq.format();
        findWxArticlesReq.countStartIndex();
        List<WxArticleVo> list = dtNewsMapper.findWxArticles(findWxArticlesReq);
        return list;
    }

    /**
     * 获取微信文章总条数
     *
     * @param findWxArticlesReq
     * @return
     */
    @Override
    public Integer findWxArticlesTotal(FindWxArticlesReq findWxArticlesReq) throws SzException {
        findWxArticlesReq.format();
        return dtNewsMapper.findWxArticlesTotal(findWxArticlesReq);
    }

    /**
     * 修改微信文章显示状态
     *
     * @param wxArticlesIsVisible
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWxArticlesIsVisible(WxArticlesIsVisibleReq wxArticlesIsVisible) throws SzException {
        wxArticlesIsVisible.format();


        //从数据库中获取出文章对象
        WxArticleVo wxArticleVo = dtNewsMapper.findWxArticleInfo(wxArticlesIsVisible.getId());
        if (null == wxArticleVo) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有找到该文章");
        }
        if (1 == wxArticleVo.getIsVisible()) {
            wxArticleVo.setIsVisible(0);
        } else {
            wxArticleVo.setIsVisible(1);
        }
        dtNewsMapper.updateWxArticlesIsVisible(wxArticleVo);
        //获取当前登入用户
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        //添加日志
        LogUtil.buildUserLog(wxArticleVo.getId() + "",
                " 【文章管理】用户 {" + currentUser.getUsername() +
                        "} 修改了微信文章(" + wxArticleVo.getId() + ")的展示状态为（" + wxArticleVo.getIsVisible() + "） "
                , currentUser, null);
    }

    /**
     * 修改微信文章所属社区
     *
     * @param updateWxArticleAreaReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWxArticleArea(UpdateWxArticleAreaReq updateWxArticleAreaReq) throws SzException {
        updateWxArticleAreaReq.format();
        //通过id查询获取需要操作的文章对象
        WxArticleVo wxArticleVo = dtNewsMapper.findWxArticleInfo(updateWxArticleAreaReq.getId());
        if (null == wxArticleVo) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有找到该文章");
        }

        wxArticleVo.setCid(updateWxArticleAreaReq.getCid());
        wxArticleVo.setFromToAreaId(updateWxArticleAreaReq.getCid());

        //修改微信文章的所属社区
        dtNewsMapper.updateWxArticleArea(wxArticleVo);

        //获取当前登入用户
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        //添加日志
        LogUtil.buildUserLog(wxArticleVo.getId() + "",
                " 【文章管理】用户 {" + currentUser.getUsername() +
                        "} 修改了微信文章(" + wxArticleVo.getId() + ")的所属社区（" + wxArticleVo.getCid() + "） "
                , currentUser, null);
    }

    /**
     * 逻辑删除文章
     *
     * @param removeArticleAreaReq
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeArticleArea(RemoveArticleAreaReq removeArticleAreaReq) throws SzException {
        //通过id查询获取需要操作的文章对象
        WxArticleVo wxArticleVo = dtNewsMapper.findWxArticleInfo(removeArticleAreaReq.getId());
        if (null == wxArticleVo) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "没有找到该文章");
        }
        if (wxArticleVo.getNewType() == 2) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "微信文章不可删除");
        }

        dtNewsMapper.removeArticleArea(removeArticleAreaReq);
        //获取当前登入用户
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        //添加日志
        LogUtil.buildUserLog(wxArticleVo.getId() + "",
                " 【文章管理】用户 {" + currentUser.getUsername() +
                        "} 删除了微信文章 (" + wxArticleVo.getId() + ") 《" + wxArticleVo.getTitle() + "》", currentUser, null);

    }

    /**
     * 获取党政新闻列表
     *
     * @param findPartyPolicyNewsReq
     * @return
     */
    @Override
    public FindPartyPolicyNewsResponse findPartyPolicyNews(FindPartyPolicyNewsReq findPartyPolicyNewsReq) {
        findPartyPolicyNewsReq.countStartIndex();
        List<WxArticleVo> articleVos = dtNewsMapper.findPartyPolicyNews(findPartyPolicyNewsReq);
        FindPartyPolicyNewsResponse response = new FindPartyPolicyNewsResponse();
        response.setPartyPolicyNews(articleVos);
        return response;
    }

    /**
     * 查询用户的管辖地区
     *
     * @return
     */
    @Override
    public List<DtNewsSort> findSortList(Integer type) throws SzException {
        if (type.equals(ContentConstant.ZJ_DW) || type.equals(ContentConstant.ZJ_ZW)) {
            List<DtNewsSort> town = DtUsersVo.findCurrentUser().findUserManageTown();
            return town;
        } else if (type.equals(ContentConstant.CJ_DW) || type.equals(ContentConstant.CJ_CW)) {
            List<DtNewsSort> area = DtUsersVo.findCurrentUser().findUserManageArea();
            List<DtNewsSort> community = DtUsersVo.findCurrentUser().findUserManageCommunity();
            area.addAll(community);
            return area;
        } else {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "参数错误");
        }
    }

    @Override
    public List<DtNewsSort> findUserManageSortByPid(Integer pid) {

        if (pid == null || pid == -1) {
            pid = -1;
            List<DtNewsSort> sortParent = findSortParent();
            if (!CollectionUtils.isEmpty(sortParent)) {
                pid = sortParent.get(0).getId();
            }
        }
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        if (SystemRoleConstant.Z_JGZ_CY.equals(currentUser.getUserMaxRole()) || SystemRoleConstant.Z_JGZ_ZZ.equals(currentUser.getUserMaxRole())) {
            return SpringContextConfig.getBean(MenuMapper.class).selectNewsSortByPid(pid);
        } else {
            return SpringContextConfig.getBean(MenuMapper.class).findUserManageArea(currentUser.getId(), pid);
        }
    }

    @Override
    public List<DtNewsSort> findSortParent() {
        DtUsersVo currentUser = DtUsersVo.findCurrentUser();
        return currentUser.findSortParent();
    }

    /**
     * 查询昌东镇的文章类型
     *
     * @param type
     * @return
     */
    @Override
    public List<CdzSystemType> findCdzType(Integer type) {
        //1.定义一个变量
        Integer pid = null;
        if (type.equals(ContentConstant.ZJ_DW)) {
            pid = 1;
        } else if (type.equals(ContentConstant.ZJ_ZW)) {
            pid = 2;
        } else if (type.equals(ContentConstant.CJ_DW)) {
            pid = 4;
        } else if (type.equals(ContentConstant.CJ_CW)) {
            pid = 5;
        }
        List<CdzSystemType> typeList = dtNewsMapper.findCdzContentType(pid);
        return typeList;
    }

    /**
     * 公开文章(取消公开)
     *
     * @param contentId
     */
    @Override
    public Boolean publicContent(Integer contentId, Integer type) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        //获取用户的角色
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        Integer i = dtNewsMapper.findPublic(contentId);

        //角色校验
        CheckOperationArticleRoleStrategy roleStrategy = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(type);
        if (!roleStrategy.release(userMaxRole)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "暂无权限");
        }


        //修改文章公开状态
        dtNewsMapper.auditContent(contentId, i = (i == 1 ? 0 : 1));

        //创建文章审核记录对象
        CdzAuditRecord cdzAuditRecord = new CdzAuditRecord();
        cdzAuditRecord.setJoinId(contentId);
        cdzAuditRecord.setIsPublic(i);
        cdzAuditRecord.setLastCommandTime(new Date());
        cdzAuditRecord.setSubmitTime(new Date());
        cdzAuditRecord.setSubmitUserId(dtUsersVo.getId());
        cdzAuditRecord.setAuditType(roleStrategy.roleAuditType());
        cdzAuditRecord.setCurrentStatus(OneLevelAuditConstant.PUBLIC);

        dtNewsMapper.insertRecord(cdzAuditRecord);
        return Boolean.TRUE;
    }

    /**
     * 转换草稿
     *
     * @param contentId
     */
    @Transactional(rollbackFor = Exception.class)
    public void convertDrafts(Integer contentId, Integer type) throws SzException {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        //获取用户的角色
        Integer userMaxRole = dtUsersVo.findUserMaxRole();

        //角色校验
        CheckOperationArticleRoleStrategy roleStrategy = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(type);
        if (!roleStrategy.edit(userMaxRole)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "暂无权限");
        }
        DtNewsVo dtNewsVo = dtNewsMapper.selectNewsById(contentId);
        if (!dtNewsVo.getAuditStatus().equals(OneLevelAuditConstant.FAIL)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "当前状态，不能进行转草稿");
        }
        dtNewsMapper.updateNewsStatusById(contentId, OneLevelAuditConstant.DRAFT);

        //创建文章审核记录对象
        CdzAuditRecord cdzAuditRecord = new CdzAuditRecord();
        cdzAuditRecord.setJoinId(contentId);
        cdzAuditRecord.setIsPublic(0);
        cdzAuditRecord.setLastCommandTime(new Date());
        cdzAuditRecord.setSubmitTime(new Date());
        cdzAuditRecord.setSubmitUserId(dtUsersVo.getId());
        cdzAuditRecord.setAuditType(roleStrategy.roleAuditType());
        cdzAuditRecord.setCurrentStatus(OneLevelAuditConstant.DRAFT);
        dtNewsMapper.insertRecord(cdzAuditRecord);
    }

    /**
     * 提交审核
     *
     * @param contentId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitAudit(Integer contentId, Integer type) throws SzException {

        //定义不审核(直接通过的系统类型)的文章
        List<Integer> systemTypeNoAuditIds = Arrays.asList(50);
        DtNewsVo dtNewsVo = dtNewsMapper.selectNewsById(contentId);
        if (dtNewsVo == null) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "文章不存在 无法提交");
        }
        Integer operationState = OneLevelAuditConstant.SUBMIT_AUDIT;

        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        //获取用户的角色
        Integer userMaxRole = dtUsersVo.findUserMaxRole();

        //二务（镇党务、镇政务、村党务、村村务）校验角色操作权限
        CheckOperationArticleRoleStrategy roleAdapter = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(type);
        if (roleAdapter == null || !roleAdapter.submit(userMaxRole)) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "暂无权限");
        }

        if (systemTypeNoAuditIds.contains(dtNewsVo.getSystemTypeId())) {
            operationState = OneLevelAuditConstant.CONSENT;
            //创建文章审核记录对象
            CdzAuditRecord cdzAuditRecord = new CdzAuditRecord();
            cdzAuditRecord.setJoinId(contentId);
            cdzAuditRecord.setSubmitTime(new Date());
            cdzAuditRecord.setLastCommandTime(new Date());
            cdzAuditRecord.setSubmitUserId(dtUsersVo.getId());
            cdzAuditRecord.setAuditType(roleAdapter.roleAuditType());
            cdzAuditRecord.setCurrentStatus(OneLevelAuditConstant.SUBMIT_AUDIT);
            cdzAuditRecord.setIsPublic(0);
            dtNewsMapper.insertRecord(cdzAuditRecord);
        }
        dtNewsMapper.submitAudit(contentId, operationState);

        //创建文章审核记录对象
        CdzAuditRecord cdzAuditRecord = new CdzAuditRecord();
        cdzAuditRecord.setJoinId(contentId);
        cdzAuditRecord.setSubmitTime(new Date());
        cdzAuditRecord.setLastCommandTime(new Date());
        cdzAuditRecord.setSubmitUserId(dtUsersVo.getId());
        cdzAuditRecord.setAuditType(roleAdapter.roleAuditType());
        cdzAuditRecord.setCurrentStatus(operationState);
        cdzAuditRecord.setIsPublic(0);
        dtNewsMapper.insertRecord(cdzAuditRecord);

        return Boolean.TRUE;
    }

    /**
     * 查询文章数据
     *
     * @param type
     * @return
     */
    @Override
    public ContentListDataVo findContentData(Integer type) {
        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();
        //获取用户的角色
        Integer userMaxRole = dtUsersVo.findUserMaxRole();
        String areaId = null;
        if (ContentConstant.ZJ_ZW.equals(type) || ContentConstant.ZJ_DW.equals(type)) {
            areaId = dtUsersVo.findUserManageTownId();
        } else if (ContentConstant.CJ_DW.equals(type) || ContentConstant.CJ_CW.equals(type)) {
            if (dtUsersVo.findUserManageAreaId().equals("-111")) {
                areaId = dtUsersVo.findUserManageCommunityId();
            } else {
                areaId = dtUsersVo.findUserManageAreaId();
            }
        }
        ContentListDataVo contentListDataVo = new ContentListDataVo();
        //1.查询总条数
        contentListDataVo.setAllTotal(dtNewsMapper.findContentAuditStatusCount(type, null, areaId));
        //2.查询未审核条数
        contentListDataVo.setWaitAudit(dtNewsMapper.findContentAuditStatusCount(type, OneLevelAuditConstant.DRAFT, areaId));
        //3.查询审核中的条数
        contentListDataVo.setAuditing(dtNewsMapper.findContentAuditStatusCount(type, OneLevelAuditConstant.SUBMIT_AUDIT, areaId));
        //4.查询已通过的条数
        contentListDataVo.setAlreadyAudit(dtNewsMapper.findContentAuditStatusCount(type, OneLevelAuditConstant.CONSENT, areaId));
        return contentListDataVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditDtNews(AuditNewsDto auditNewsDto) throws SzException {

        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();

        //校验审核结果信息
        if (auditNewsDto.getIsConsent().equals(0) && !StringUtils.hasLength(auditNewsDto.getFailMsg())) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "请填写拒绝理由");
        } else if (auditNewsDto.getIsConsent().equals(1)) {
            auditNewsDto.setFailMsg(null);
        }

        String[] newIdsArr = auditNewsDto.getNewsIds().split(",");

        for (String newId : newIdsArr) {

            Integer newIdInt = Integer.parseInt(newId);
            DtNewsVo dtNews = dtNewsMapper.findDtNewsById(newIdInt);
            if (dtNews == null) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "文章查询失败");
            } else if (!dtNews.getAuditStatus().equals(OneLevelAuditConstant.SUBMIT_AUDIT)) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "当前状态不能进行审核");
            }

            //获取当前角色校验当前角色
            Integer currentUserRoleId = dtUsersVo.getUserMaxRole();
            CheckOperationArticleRoleStrategy roleStrategy = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(dtNews.getContentType());
            boolean checkResult = roleStrategy.oneLevelAudit(currentUserRoleId);
            if (!checkResult || (!StringUtils.isEmpty(dtNews.getMediaId()) && Arrays.asList(dtNews.getMediaId().toString().split(",")).contains(currentUserRoleId))) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "您当前的角色无法审核");
            }

            SystemRoleAuditAreaEnum instance = SystemRoleAuditAreaEnum.getInstance(currentUserRoleId);
            if (!instance.getMANAGE_AUDIT_SYSTEM_TYPE_ID().contains(dtNews.getSystemTypeId())) {
                throw new SzException(SystemCode.QUERY_CHECK_ERROR, "您当前角色不属于该文章");
            }
//
//        List<Integer> expectUserRoleArr = null;
//
//        if (dtNews.getContentType().equals(ContentConstant.CJ_DW)) {
//            expectUserRoleArr = Arrays.asList(SystemRoleConstant.Z_DJB_CY, SystemRoleConstant.Z_DJB_ZR);
//        } else if (dtNews.getContentType().equals(ContentConstant.CJ_CW)) {
////            expectUserRoleArr = Collections.singletonList(SystemRoleConstant.XG_ZN_GM);
//            expectUserRoleArr = Arrays.asList(
//                    SystemRoleConstant.Z_MZB,
//                    SystemRoleConstant.Z_NF_A_ZB,
//                    SystemRoleConstant.Z_GG_ZY_JY_ZX,
//                    SystemRoleConstant.Z_ZS_XM_B,
//                    SystemRoleConstant.Z_NY_FW_ZX,SystemRoleConstant.Z_LB_S);
//        } else if (dtNews.getContentType().equals(ContentConstant.ZJ_ZW)) {
//            expectUserRoleArr = Collections.singletonList(SystemRoleConstant.Z_BM_FW_ZX_ZR);
//        } else if (dtNews.getContentType().equals(ContentConstant.ZJ_DW)) {
//            expectUserRoleArr = Collections.singletonList(SystemRoleConstant.Z_DJB_ZR);
//        }
//
//        if (CollectionUtils.isEmpty(expectUserRoleArr) || !expectUserRoleArr.contains(userMaxRole)) {
//            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "当前角色没有权限进行审核");
//        }

            dtNewsMapper.updateAuditStatus(newIdInt,
                    auditNewsDto.getIsConsent().equals(1) ? OneLevelAuditConstant.CONSENT :
                            OneLevelAuditConstant.FAIL,
                    auditNewsDto.getFailMsg());

            CdzAuditRecord cdzAuditRecord = new CdzAuditRecord();
            cdzAuditRecord.setAuditType(0);
            cdzAuditRecord.setJoinId(newIdInt);
            cdzAuditRecord.setSubmitUserId(dtUsersVo.getId());
            cdzAuditRecord.setSubmitTime(new Date());
            cdzAuditRecord.setCurrentStatus(auditNewsDto.getIsConsent().equals(1) ? OneLevelAuditConstant.CONSENT :
                    OneLevelAuditConstant.FAIL);
            cdzAuditRecord.setLastCommandTime(new Date());
            cdzAuditRecord.setFailMsg(auditNewsDto.getFailMsg());
            cdzAuditRecord.setIsPublic(0);
            cdzAuditRecord.setIsValid(1);
            cdzAuditRecordDao.insertSelective(cdzAuditRecord);
        }
    }

    @Autowired
    DtNewsService dtNewsService;


    @Override
    public TableData<DtNewsVo> auditNewsTable(NewsDto changeClassifyReq) throws SzException {
        changeClassifyReq.countStartIndex();

        //获取当前登陆用户的信息
        DtUsersVo dtUsersVo = DtUsersVo.findCurrentUser();

        //获取当前角色校验当前角色
        Integer currentUserRoleId = dtUsersVo.getUserMaxRole();
        CheckOperationArticleRoleStrategy roleStrategy = CheckOperationArticleRoleStrategyDirector.getRoleStrategy(changeClassifyReq.getContentType());
        boolean checkResult = roleStrategy.auditSelect(currentUserRoleId);
        if (!checkResult) {
            throw new SzException(SystemCode.QUERY_CHECK_ERROR, "您当前的角色没有审核列表");
        }

        //获取该角色能查询指定文章类型信息ID
        SystemRoleAuditAreaEnum instance = SystemRoleAuditAreaEnum.getInstance(currentUserRoleId);
        StringJoiner systemTypeIds = new StringJoiner(",");
        instance.getMANAGE_AUDIT_SYSTEM_TYPE_ID().forEach(e -> {
            systemTypeIds.add(e.toString());
        });

        // 获取管辖的地址
        String userManageAllAddressId = null;
        if (!StringUtils.hasLength(changeClassifyReq.getAreaType())) {
            // 查询当前用户应该能看到什么areaType
            List<DtNewsSort> sortParent = dtUsersVo.findSortParent(dtUsersVo.getUserMaxRole(), false);
            if (!CollectionUtils.isEmpty(sortParent)) {
                StringJoiner sj = new StringJoiner(",");
                for (DtNewsSort dtNewsSort : sortParent) {
                    if (changeClassifyReq.getPageType().equals(1) && !dtNewsSort.getId().equals(AddressConstant.ZJ)) {
                        continue;
                    } else if (changeClassifyReq.getPageType().equals(2) && dtNewsSort.getId().equals(AddressConstant.ZJ)) {
                        continue;
                    }
                    sj.add(getManageAreaId(dtNewsSort.getId().toString(), dtUsersVo));
                }
                userManageAllAddressId = sj.toString();
            } else {
                userManageAllAddressId = "-1111";
            }
        } else {
            userManageAllAddressId = getManageAreaId(changeClassifyReq.getAreaType(), dtUsersVo);
        }

        changeClassifyReq.format();
        if (StringUtils.hasLength(changeClassifyReq.getMonthTime())) {
            changeClassifyReq.setMaxMonthTime(MD5Utils.monthLastDayTime(changeClassifyReq.getMonthTime()));
        }

        //直接指定某一个村/社区/居委区域ID
        if (!StringUtils.isEmpty(changeClassifyReq.getFromToAreaIds())) {
            userManageAllAddressId = changeClassifyReq.getFromToAreaIds();
        }

        // 查询总条数信息，和对应的信息列表
        List<DtNewsVo> dtNewsVoList = null;
        Integer count = changeClassifyReq.getIsAlreadyProcess().equals(0) ? dtNewsMapper.findAuditTableCount(changeClassifyReq,
                StringUtils.hasLength(userManageAllAddressId) ? userManageAllAddressId : "-111",
                systemTypeIds.length() < 1 ? null : systemTypeIds.toString(), currentUserRoleId) :
                dtNewsMapper.findAuditTableCount2(changeClassifyReq,
                        StringUtils.hasLength(userManageAllAddressId) ? userManageAllAddressId : "-111",
                        systemTypeIds.length() < 1 ? null : systemTypeIds.toString(), currentUserRoleId,
                        dtUsersVo.getId());
        if (count != null && count > 0) {
            dtNewsVoList = changeClassifyReq.getIsAlreadyProcess().equals(0) ? dtNewsMapper.findAuditTable(changeClassifyReq,
                    StringUtils.hasLength(userManageAllAddressId) ? userManageAllAddressId : "-111",
                    systemTypeIds.length() < 1 ? null : systemTypeIds.toString(), currentUserRoleId) :
                    dtNewsMapper.findAuditTable2(changeClassifyReq,
                            StringUtils.hasLength(userManageAllAddressId) ? userManageAllAddressId : "-111",
                            systemTypeIds.length() < 1 ? null : systemTypeIds.toString(), currentUserRoleId,
                            dtUsersVo.getId());
        }

        return new TableData<>(dtNewsVoList, count);
    }

    private static String getManageAreaId(String areaType, DtUsersVo dtUsersVo) {
        String userManageAllAddressId = null;
        if (!StringUtils.hasLength(areaType)) {
            userManageAllAddressId = dtUsersVo.findUserManageAllAddressId();
        } else if (areaType.equals("3")) {
            userManageAllAddressId = dtUsersVo.findUserManageAreaId();
        } else if (areaType.equals("16")) {
            userManageAllAddressId = dtUsersVo.findUserManageCommunityId();
        } else if (areaType.equals("4")) {
            userManageAllAddressId = dtUsersVo.findUserManageJuWeiId();
        } else if (areaType.equals("2")) {
            userManageAllAddressId = dtUsersVo.findUserManageTownId();
        }
        return userManageAllAddressId;
    }
}
